package com.example.service;

import com.example.entity.Floor;
import com.example.entity.Student;
import com.example.mapper.FloorMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class FloorService {
    @Autowired
    private FloorMapper floorMapper;
    private boolean existsInDb(Floor floor) {
        List<Floor> floorList = floorMapper.findAllByid(floor.getFloor_num());
        return !floorList.isEmpty();
    }
    public int save(Floor floor) {
        if (existsInDb(floor)) {
            return floorMapper.update(floor);
        } else {
            return floorMapper.insert(floor);
        }
    }
    private boolean existsInDb1(Floor floor) {
        List<Floor> floorList = floorMapper.findAllByid(floor.getFloor_num());
        return !floorList.isEmpty();
    }
    public int save1(Floor floor) {
        if (existsInDb(floor)) {
            return floorMapper.update(floor);
        } else {
            return floorMapper.insert(floor);
        }
    }

    public Map<String, Object> saveFloor(Floor floor) {
        Map<String, Object> result = new HashMap<>();

        Integer saveResult = save1(floor);

        if (saveResult != null) {
            result.put("code", 200);
            result.put("message", "保存成功");
            Map<String, Object> data = new HashMap<>();
            data.put("status", "UP22");
            result.put("data", data);
        } else {
            result.put("code", 50000);
            result.put("message", "保存失败");
            result.put("data", null);
        }

        return result;
    }

    public Map<String, Object> deleteBy(String floor_num) {
        Map<String, Object> result = new HashMap<>();

        Integer deleteResult = floorMapper.deleteBy(floor_num);

        if (deleteResult != null) {
            result.put("code", 200);
            result.put("message", "删除成功");
            Map<String, Object> data = new HashMap<>();
            data.put("status", "UP22");
            result.put("data", data);
        } else {
            result.put("code", 50000);
            result.put("message", "删除失败");
            result.put("data", null);
        }

        return result;
    }
}
